var modalBlocker;
var modals;

function setBehaviors(el){
	var els=el.getElementsByTagName("*");
	for(var i=0;i<els.length;i++){
		if(els[i].className=="panel"){
			setPanel(els[i]);
		}
		if(els[i].className=="autocomplete"){
			setAutoComplete(els[i]);
		}
		if(els[i].className=="grid"){
			setGrid(els[i]);
		}	
	}
}

var openedModal;
function openModal(modal){
	modalBlock();
	if(openedModal){
		openedModal.style.display="none";
	}
	if(!modals){
		modals=new Array();
	}
	openedModal=modal.cloneNode(true);
	document.body.appendChild(openedModal);
	setBehaviors(openedModal);
	openedModal.style.position="absolute";
	openedModal.style.display="block";
	openedModal.style.zIndex=999;
	openedModal.style.top=((getStageHeight()/2)-(openedModal.offsetHeight/2))+"px";
	openedModal.style.left=((getStageWidth()/2)-(openedModal.offsetWidth/2))+"px";
	modals.push(openedModal);
}

function closeOpenedModal(){
	modalUnBlock();
	openedModal.parentNode.removeChild(openedModal);
	modals.splice(modals.length-1,1);
	if(modals.length>0){
		openedModal=modals[modals.length-1];
		openedModal.style.display="block";
		modalBlock();
	}else{
		openedModal=null;
	}
}

function modalBlock(){
	if(!modalBlocker){
		modalBlocker=document.createElement("DIV");
		document.body.appendChild(modalBlocker);
		modalBlocker.className="opacity50";
		modalBlocker.style.backgroundColor="#000000";
		modalBlocker.style.position="absolute";
	}
	modalBlocker.style.display="block";
	modalBlocker.style.top="0px";
	modalBlocker.style.height=getStageHeight()+"px";
	modalBlocker.style.width=getStageWidth()+"px";
	modalBlocker.style.zIndex=888;
}

function modalUnBlock(){
	if(modalBlocker){
		modalBlocker.style.display="none";
	}
}